﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using BeachRunner.Utility;
using BeachRunner.Tileengine;

using BeachRunner.Entity;

namespace BeachRunner.BeachRunner
{
    /// <summary>
    /// Partikel für den Wasserverlust wenn der Spieler ein Hinderniss trifft.
    /// 
    /// Autor: OLD (Mattis Hänel)
    /// </summary>
    class Water : Entity.Entity
    {
        /// <summary>
        /// Die Grafik des Wasserpartikels
        /// </summary>
        private static SpriteSet spriteSet;

        /// <summary>
        /// Die Animationsabläufe
        /// </summary>
        private static short[][] indexes;

        /// <summary>
        /// Die Animationszeiten
        /// </summary>
        private static short[][] times;

        /// <summary>
        /// Wie offt ist das Partikel schon auf dem Boden aufgeschalgen?
        /// </summary>
        private int groundCollisions;

        public static void LoadContent( ContentManager contentManager )
        {
            indexes = new short[1][];
            indexes[0] = new short[] { 0, 1, 2, 3, 4, 5 };

            times = new short[1][];
            times[0] = new short[] { 70, 70, 70, 70, 70, 70 };

            spriteSet = new SpriteSet(contentManager.Load<Texture2D>("gfx/water"), 24, 10);
        }

        public Water(EntitySet entitySet, Vector2 position, Vector2 direction, Layer collisonLayer )
            : base(entitySet, position, 24, 10, null, collisonLayer)
        {
            animationSet = new AnimationSet(spriteSet, indexes, times);
            animationSet.IsLooping = false;

            velocity = direction;
            maximalVelocity = new Vector2(32, 32);
            bounciness = new Vector2(0.6f, 0.6f);
            friction = new Vector2( 0.15f, 0.0f );
        }

        /// <summary>
        /// Bei der vierten Bodenberührung wird das Partikel entfernt.
        /// </summary>
        /// <param name="result"></param>
        override public void HandleTraceResult(TraceResult result)
        {
            base.HandleTraceResult(result);

            if (result.collisionY)
            {
                groundCollisions++;
            }

            if (groundCollisions == 4)
            {
                animationSet.PlayAnimation();
            }

            if (groundCollisions > 4 && animationSet.FrameID == animationSet.Frames - 1)
            {
                entitySet.Remove(this);
            }
        }
    }
}
